package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_fragment.bp_home

import com.bp.hearthealth.pulse.lnapps.bp_db.bp_project.BpDataBaseManager
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_network.BaseRepository
import com.bp.hearthealth.pulse.lnapps.bp_network.bp_api.ApiBpService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext

class BpHomeRepository(private val service: ApiBpService) : BaseRepository() {

    suspend fun loadHeartRateBean(userId: String) = withContext(Dispatchers.IO) {
        coroutineScope {
            coroutineScope {
                try {
                    val heartRatesList = BpDataBaseManager.getInstance().getHeartRate()
                        .getHeartRatesListByUserId(userId = userId)
                    if (heartRatesList.isNotEmpty()) {
                        heartRatesList.first()
                    } else {
                       null
                    }
                } catch (e: Exception) {
                    LogUtil.e(e)
                    null
                }
            }
        }
    }
}